# I18n Module 
# License: http://www.apache.org/licenses/LICENSE-2.0.html 
# URL: http://www.reality.hk/articles/2007/02/20/681/ 
# 
# Modified from Localization module at mir.aculo.us 
# http://mir.aculo.us/2005/10/03/ruby-on-rails-i18n-revisited 
# 
# Thanks Takol for idea to use HTTP_ACCEPT_LANGUAGE to select language
# http://takol.tw/data/326894672446572cc5.html
 
module I18n 
  mattr_accessor :lang 
  PARAMETER_LANG = 'lang'
  
  @@l10s = { :default => {} } 
  @@lang = :default 

  def lang_filter
    usr_lang = self.request.parameters[PARAMETER_LANG] ||= request.env['HTTP_ACCEPT_LANGUAGE'].to_s.scan(/\w*\-\w*/).first
    I18n.lang = I18n.lang?(usr_lang) ? usr_lang : :default 
  end

  def self.included(base) #:nodoc:
    base.send(:before_filter, :lang_filter)
  end
  
  def self._(string_to_localize, *args) 
    translated = 
      @@l10s[@@lang][string_to_localize] || string_to_localize 
    return translated.call(*args).to_s if translated.is_a? Proc 
    translated = 
      translated[args[0]>1 ? 1 : 0] if translated.is_a?(Array) 
    sprintf translated, *args 
  end 
 
  def self.load 
    loaded = [] 
    Dir.glob("#{RAILS_ROOT}/config/lang/*.yml"){ |yml| 
      name = File.basename(yml, ".yml") 
      translate = YAML.load_file(yml) 
      I18n.define(name, translate) 
      loaded << name 
    }
    printf("=> mob_i18n: loaded languages: #{loaded.join(', ')}\n")
    return loaded 
  end 
 
  def self.define(lang = :default, dictionary = {}) 
    @@l10s[lang] ||= dictionary 
  end 
 
  def self.lang?(lang) 
    @@l10s.key?(lang) 
  end 
end 

class Object 
  def _(*args); I18n._(*args); end 
end